********Create lifetime experienced real GDP growth mean and real GDP growth standard deviation, based on state of residence********

*Clean GDP growth data

cd "$reploc/data"
use Mex_prov_timeseries.dta, clear

*Winsorize province growth data at 5-95 level across all provinces
reshape long gdpgr, i(provcode) j(year)
winsor gdpgr, p(0.05) gen(gdpgr_wins)
drop gdpgr
rename gdpgr_wins gdpgr
reshape wide gdpgr, i(provcode) j(year)


tempfile prov_timeseries_winsmex
save "`prov_timeseries_winsmex'", replace

*Merge residence province data, and create location data
cd "$reploc/data/created_data"
use indiv_prov_hist_mexico, clear
keep pid_link mxfls
tempfile indiv_timeseriesmex
save "`indiv_timeseriesmex'", replace


*Province data based on location
forv i = 1941/2014{
	cd "$reploc/data/created_data"
    use indiv_prov_hist_mexico, clear
	gen provcode = state`i'
	merge m:1 provcode using "`prov_timeseries_winsmex'", gen(_merge1)
	drop if _merge1 == 2	
	rename gdpgr`i' grex`i'
	keep pid_link mxfls grex* 
	merge 1:1 pid_link mxfls using "`indiv_timeseriesmex'", gen(_merge2)
	drop _merge2
	save "`indiv_timeseriesmex'", replace
}



*Merging in other variables in indiv_provgrbir_stats_mex we care about
preserve
cd "$reploc/data/created_data"
use indiv_provgrbir_stats_mex, clear
keep pid_link mxfls yearinterviewmx2 yearinterviewmx3 birthstate birthyr
tempfile r3
save "`r3'"
restore

merge 1:1 pid_link mxfls using "`r3'", gen(_mergebir) keepus(yearinterviewmx2 mxfls yearinterviewmx3 birthstate birthyr)
keep if _mergebir==3

keep pid_link mxfls grex* birthstate birthyr yearinterviewmx2 yearinterviewmx3

***Generate experienced mean and volatility of growth
egen yearinterview12=min(yearinterviewmx2), by(pid_link)
egen yearinterview13=min(yearinterviewmx3), by(pid_link)

replace yearinterviewmx2=yearinterview12 if mxfls==3 
replace yearinterviewmx3=yearinterview13 if mxfls==2 

gen imp_agemx2 = yearinterviewmx2 - birthyr
gen imp_agemx3 = yearinterviewmx3 - birthyr

duplicates drop pid_link, force


*Merge in national timesieres
gen idmex=1

***This imports gdp in each year since 1925 for each person 
cd "$reploc/data"
merge m:1 idmex using Mex_timeseries.dta, gen(_mergemex)
drop idmex
drop _mergemex


capture drop grex1940
local t grex

quietly {

order grex*, sequential
local nums mx2 mx3




*Variation 1: Assume missing for individuals born before 1941
	local nums mx2 mx3
	local lam1=0
	local lam2=0
	local numi=5

	forval lambda1=`lam1'(10)`lam2' {
	
	
	capture drop grex????_wt1mx?_lam* sumweights* grex????lam*_mx? grex????lam*sd_* intSD_lam*_41*
	
		local lambda=round(`lambda1'/100,0.1)


		if `lambda'<0 {
			local hola=substr("`lambda1'",2,.)
			local lamnam neg`hola'
		}
		else {
			local lamnam `lambda1'
		}


		*Avg
		foreach wav in `nums' {
			foreach var of varlist `t'1941-`t'2014  {

				local curryear=substr("`var'",`numi',.)
		
				
				local k=yearinterview`wav'-`curryear'
				gen `var'_wt1`wav'_lam`lamnam'=.
			
				replace `var'_wt1`wav'_lam`lamnam'=((imp_age`wav'-`k')/imp_age`wav')^`lambda' if `k'<=imp_age`wav' & `curryear'<=yearinterview`wav' & birthyr>=1941 & birthyr<=`curryear'
         
			   replace `var'_wt1`wav'_lam`lamnam'=((imp_age`wav'-`k')/imp_age`wav')^`lambda' if birthyr<=1976 & `curryear'<=yearinterview`wav'
			   
				
			}
		


			egen sumweights`wav'_lam`lamnam'=rowtotal(*_wt1`wav'_lam`lamnam')
			


			foreach var of varlist `t'???? {

				local curryear=substr("`var'",`numi',.)
				gen `var'lam`lamnam'_`wav'=.
				replace `var'lam`lamnam'_`wav'=`var'*`var'_wt1`wav'_lam`lamnam'/sumweights`wav'_lam`lamnam' if birthyr<=`curryear' & birthyr>=1941
                replace `var'lam`lamnam'_`wav'=`var'*`var'_wt1`wav'_lam`lamnam'/sumweights`wav'_lam`lamnam' if birthyr<=`curryear' & birthyr<1941
				
			}
		}

		egen `t'provAvg_lam`lamnam'_41mx2=rowtotal(`t'????lam`lamnam'_mx2), missing
		egen `t'provAvg_lam`lamnam'_41mx3=rowtotal(`t'????lam`lamnam'_mx3), missing

		*SD

		foreach wav in `nums' {
			foreach var of varlist `t'???? {
			
				local curryear=substr("`var'",`numi',.)
				gen `var'lam`lamnam'sd_`wav'=.
				replace `var'lam`lamnam'sd_`wav'=`var'_wt1`wav'_lam`lamnam'*(`var'-`t'provAvg_lam`lamnam'_41`wav')^2 if birthyr<=`curryear' & birthyr>=1941
				replace `var'lam`lamnam'sd_`wav'=`var'_wt1`wav'_lam`lamnam'*(`var'-`t'provAvg_lam`lamnam'_41`wav')^2 if birthyr<=`curryear' & birthyr<1941
				
			}
		}
		
		capture drop intSD_lam*

		egen intSD_lam`lamnam'_41mx2=rowtotal(`t'????lam`lamnam'sd_mx2), missing
		egen intSD_lam`lamnam'_41mx3=rowtotal(`t'????lam`lamnam'sd_mx3), missing


		gen `t'provSD_lam`lamnam'_41mx2=sqrt((imp_agemx2)*intSD_lam`lamnam'_41mx2/((imp_agemx2-1)*sumweightsmx2_lam`lamnam'))
		gen `t'provSD_lam`lamnam'_41mx3=sqrt((imp_agemx3)*intSD_lam`lamnam'_41mx3/((imp_agemx3-1)*sumweightsmx3_lam`lamnam'))


		capture drop grex????lam* intSD* *_wt1*_lam* sumweights*
		
	}
	
	
	
	capture drop grex????lam*_?? intSD* *_wt1*_lam* sumweights*
	
	*Variation 2: Attach mexican growth for people who are born before 1941

local nums mx2 mx3
	local lam1=0
	local lam2=0
	local numi=5

	
	
	forval lambda1=`lam1'(10)`lam2' {
	
	
	capture drop *_wt1mx2_lam* *_wt1mx3_lam* sumweights* *lam*_mx2 *lam*_mx3 
	
		local lambda=round(`lambda1'/100,0.1)


		if `lambda'<0 {
			local hola=substr("`lambda1'",2,.)
			local lamnam neg`hola'
		}
		else {
			local lamnam `lambda1'
		}


		*Avg
		foreach wav in `nums' {
		
		
		forval curryear=1925/2014 {
				
				local k=yearinterview`wav'-`curryear'
				gen v`curryear'_wt1`wav'_lam`lamnam'co2=.
			
				replace v`curryear'_wt1`wav'_lam`lamnam'co2=((imp_age`wav'-`k')/imp_age`wav')^`lambda' if `k'<=imp_age`wav' & `curryear'<=yearinterview`wav' & birthyr>=1925 & birthyr<=`curryear'
				
		
			
				
			}
		


			egen sumweights`wav'_lam`lamnam'co2=rowtotal(*_wt1`wav'_lam`lamnam'co2)
			



				forval curryear=1925/2014 {

				
				gen v`curryear'lam`lamnam'_`wav'co2=.
				
				
				if `curryear'>=1941 {
				
				replace v`curryear'lam`lamnam'_`wav'co2=`t'`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co2/sumweights`wav'_lam`lamnam'co2 if birthyr<=`curryear' & birthyr>=1925
				
				
				}
				
				if `curryear'<1941 {
				
								replace v`curryear'lam`lamnam'_`wav'co2=mex_gr`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co2/sumweights`wav'_lam`lamnam'co2 if birthyr<=`curryear' & birthyr>=1925 & birthyr<=1940
				
				}
				
			}
		}

		egen `t'provAvg_lam`lamnam'_41mx2co2=rowtotal(v????lam`lamnam'_mx2co2), missing
		egen `t'provAvg_lam`lamnam'_41mx3co2=rowtotal(v????lam`lamnam'_mx3co2), missing

*SD

		foreach wav in `nums' {
		forval curryear=1925/2014 {
				gen v`curryear'lam`lamnam'sd_`wav'co2=.
				
				
						if `curryear'>=1941 {
				
				replace v`curryear'lam`lamnam'sd_`wav'co2=v`curryear'_wt1`wav'_lam`lamnam'co2*(`t'`curryear'-`t'provAvg_lam`lamnam'_41`wav'co2)^2 if birthyr<=`curryear' & birthyr>=1925 


				}
				
					if `curryear'<1941 {
					
				replace v`curryear'lam`lamnam'sd_`wav'co2=v`curryear'_wt1`wav'_lam`lamnam'co2*(mex_gr`curryear'-`t'provAvg_lam`lamnam'_41`wav'co2)^2 if birthyr<=`curryear' & birthyr>=1925  & birthyr<=1940 
							
					}
				
			}
		}
		
		capture drop intSD_lam*

		egen intSD_lam`lamnam'_41mx2co2=rowtotal(v????lam`lamnam'sd_mx2co2), missing
		egen intSD_lam`lamnam'_41mx3co2=rowtotal(v????lam`lamnam'sd_mx3co2), missing


		gen `t'provSD_lam`lamnam'_41mx2co2=sqrt((imp_agemx2)*intSD_lam`lamnam'_41mx2/((imp_agemx2-1)*sumweightsmx2_lam`lamnam'co2))
		gen `t'provSD_lam`lamnam'_41mx3co2=sqrt((imp_agemx3)*intSD_lam`lamnam'_41mx3/((imp_agemx2-1)*sumweightsmx3_lam`lamnam'co2))

		
		capture drop v????lam* intSD* *_wt1*_lam* sumweights*


	}
	
capture drop v????lam* intSD* *_wt1*_lam* sumweights*
	
	

	
	
	
*Variation 3: Attach Mexican growth for everyone from ages 0-15


local nums mx2 mx3


	local lam1=0
	local lam2=0
	local numi=5
	
	
	forval lambda1=`lam1'(10)`lam2' {
	
	
	capture drop *_wt1mx2_lam* *_wt1mx3_lam* sumweights* *lam*_mx2 *lam*_mx3 
	
		local lambda=round(`lambda1'/100,0.1)



		if `lambda'<0 {
			local hola=substr("`lambda1'",2,.)
			local lamnam neg`hola'
		}
		else {
			local lamnam `lambda1'
		}

*Avg
		
		foreach wav in `nums' {
		
		
		forval curryear=1925/2014 {
				
				local k=yearinterview`wav'-`curryear'
				gen v`curryear'_wt1`wav'_lam`lamnam'co3=.
			
				replace v`curryear'_wt1`wav'_lam`lamnam'co3=((imp_age`wav'-`k')/imp_age`wav')^`lambda' if `k'<=imp_age`wav' & `curryear'<=yearinterview`wav' & birthyr>=1925 & birthyr<=`curryear'
				
		
			
				
			}
		


			egen sumweights`wav'_lam`lamnam'co3=rowtotal(*_wt1`wav'_lam`lamnam'co3)
			



				forval curryear=1925/2014 {

				
				gen v`curryear'lam`lamnam'_`wav'co3=.
				
				
				if `curryear'>=1941 {
				
				replace v`curryear'lam`lamnam'_`wav'co3=`t'`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co3/sumweights`wav'_lam`lamnam'co3 if birthyr<=`curryear' & birthyr>=1925 & `curryear'-birthyr>15 
				
				replace v`curryear'lam`lamnam'_`wav'co3=mex_gr`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co3/sumweights`wav'_lam`lamnam'co3 if birthyr<=`curryear' & birthyr>=1925 & `curryear'-birthyr<=15 
				
				}
				
				if `curryear'<1941 {
				
								replace v`curryear'lam`lamnam'_`wav'co3=mex_gr`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co3/sumweights`wav'_lam`lamnam'co3 if birthyr<=`curryear' & birthyr>=1925 & `curryear'-birthyr<=15 
				
				}
				
			}
		}

		egen `t'provAvg_lam`lamnam'_41mx2co3=rowtotal(v????lam`lamnam'_mx2co3), missing
		egen `t'provAvg_lam`lamnam'_41mx3co3=rowtotal(v????lam`lamnam'_mx3co3), missing

		*SD 

		foreach wav in `nums' {
		forval curryear=1925/2014 {
				gen v`curryear'lam`lamnam'sd_`wav'co3=.
				
				
						if `curryear'>=1941 {
				
				replace v`curryear'lam`lamnam'sd_`wav'co3=v`curryear'_wt1`wav'_lam`lamnam'co3*(`t'`curryear'-`t'provAvg_lam`lamnam'_41`wav'co3)^2 if birthyr<=`curryear' & birthyr>=1925  & `curryear'-birthyr>15 

				replace v`curryear'lam`lamnam'sd_`wav'co3=v`curryear'_wt1`wav'_lam`lamnam'co3*(mex_gr`curryear'-`t'provAvg_lam`lamnam'_41`wav'co3)^2 if birthyr<=`curryear' & birthyr>=1925  & `curryear'-birthyr<=15 
				
				}
				
					if `curryear'<1941 {
					
									replace v`curryear'lam`lamnam'sd_`wav'co3=v`curryear'_wt1`wav'_lam`lamnam'co3*(mex_gr`curryear'-`t'provAvg_lam`lamnam'_41`wav'co3)^2 if birthyr<=`curryear' & birthyr>=1925  & `curryear'-birthyr<=15 
					
					}
				
			}
		}
		
		capture drop intSD_lam*

		egen intSD_lam`lamnam'_41mx2co3=rowtotal(v????lam`lamnam'sd_mx2co3), missing
		egen intSD_lam`lamnam'_41mx3co3=rowtotal(v????lam`lamnam'sd_mx3co3), missing



		gen `t'provSD_lam`lamnam'_41mx2co3=sqrt((imp_agemx2)*intSD_lam`lamnam'_41mx2/((imp_agemx2-1)*sumweightsmx2_lam`lamnam'co3))
		gen `t'provSD_lam`lamnam'_41mx3co3=sqrt((imp_agemx3)*intSD_lam`lamnam'_41mx2/((imp_agemx3-1)*sumweightsmx3_lam`lamnam'co3))

		capture drop v????lam* intSD* *_wt1*_lam* sumweights*

	}
	
	capture drop v????lam* intSD* *_wt1*_lam* sumweights*
	



}


keep pid_link grex???? *provAvg* *provSD* birthstate birthyr

preserve
drop *mx3*
gen mxfls=2
tempfile grs1res
save "`grs1res'", replace
restore

drop *mx2*
gen mxfls=3
append using "`grs1res'"




